#!/usr/bin/env python39
# -*- coding:utf-8 -*-
from copyheaders import headers_raw_to_dict
from requests_html import HTMLSession

str = b'''
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36
'''


class King:
    def __init__(self):
        self.url = 'http://www.baojinews.com/wenhua/index.html'
        self.base_url = 'http://www.baojinews.com/wenhua/index_{}.html'
        self.session = HTMLSession()
        self.headers = headers_raw_to_dict(str)
        self.details_list=[]

    def run(self, url):
        try:
            r = self.session.get(url=url, headers=self.headers)
            print(r.status_code, r.encoding)
            element = r.html.find('.on li .bigtit a')
            for e in element:
                link=e.attrs['href']
                self.details_list.append(link)

        except Exception as e:
            print("Error: " + str(e))

    def main(self):
        urls_list = [self.base_url.format(page) for page in range(1, 11)]
        urls_list.insert(0, self.url)
        for url in urls_list:
            self.run(url)
        for link in self.details_list:
            self.get_second_handle(link)

    def get_second_handle(self, link):
        try:
            rr=self.session.get(url=link,headers=self.headers)
            print(rr.status_code,rr.encoding)
            detail_title=rr.html.find('.g-detailtit h2:first')
            for title in detail_title:
                print(title.text)
            detail_content=rr.html.find('.g-con p')
            for cot in detail_content:
                if cot is not None:
                    print(''.join(cot.text.split()))
            detail_img=rr.html.find('.g-con p img')
            for img in detail_img:
                print(img.attrs['src'])

        except Exception as e:
            print('Error:'+str(e))


if __name__ == '__main__':
    mm = King()
    mm.main()
